home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / ident.c < prev    next >
C/C++ Source or Header  |  1985-08-10  |  4KB  |  165 lines

  1. /*  ident
  2.  
  3.     copyright (c) 1985, Gimpel Software
  4.     James F. Gimpel, President
  5.     3207 Hogarth Lane
  6.     Collegeville, PA 19426
  7.     (215) 584-4261
  8.  
  9.     Specializing in compilers, interpreters, and translators for the
  10.     IBM PC and Unix worlds.
  11.  
  12.     This product has been released to public domain for non-profit
  13.     use only. 
  14.  
  15.     The purpose of this program is to display or replace a given
  16.     identifier in a file.  It will skip over any identifier in which
  17.     the given identifier is embedded.  Thus if the given identifier
  18.     is "i" then the identifiers "hi" and "in" will be passed over.
  19.     identifiers follow the rules of C but other than this the program
  20.     knows nothing about C syntax.  In particular, it knows nothing
  21.     about comments or quotes.
  22.  
  23.     To find out how to use ident invoke it at command level with
  24.     no arguments.
  25.                     JFG   11/11/84
  26.  */
  27.  
  28. #include "stdio.h"
  29.  
  30. typedef char *STRING;
  31. #define BUFLEN 100
  32. #define id_begin(c) (isalpha(c) || c == '_')
  33. #define id_cont(c) (isalnum(c) || c == '_')
  34. #define copyc(c) if(copy_flag) putc(c,stdout)
  35. extern STRING fgets();
  36.  
  37. int copy_flag;
  38.  
  39. main(argc,argv)
  40.     int argc;
  41.     STRING  *argv;
  42.     {
  43.     int  i, j, n;
  44.     int c;
  45.     FILE  *f;
  46.     char buf[BUFLEN], newbuf[BUFLEN];
  47.     char first;
  48.     STRING  pat, pattern, replacement, p, rest;
  49.     int match, rest_len;
  50.     int lineno = 0;
  51.  
  52.     if( argc < 3 )
  53.     {
  54.     help();
  55.     }
  56.     f = fopen( argv[1], "r" );
  57.     if( !f ) error( "Can't open input file" );
  58.     pattern = argv[2];
  59.     first = pattern[0];
  60.     rest = pattern+1;
  61.     rest_len = strlen(rest);
  62.     copy_flag = argc > 3;
  63.     replacement = copy_flag ? argv[3] : "";
  64.     while( fgets(buf, BUFLEN, f ) )
  65.     {
  66.     lineno++;
  67.     p = buf;
  68.     match = 0;
  69.     while( c = *p++ )
  70.         {
  71.         if( c == first && strinb( rest, p ) &&
  72.         !id_cont( p[rest_len] )  &&
  73.         !after_id( p-1, buf ) )
  74.         {
  75.         match = 1;
  76.         copys( replacement );
  77.         p += rest_len ;
  78.         }
  79.         else copyc(c);
  80.         }
  81.     if( match && !copy_flag )
  82.         printf( "%d\t%s", lineno, buf );
  83.     }
  84.     }
  85.  
  86. /* copys(s) copies string s to stnd out (conditionally) */
  87.  
  88. copys(s)
  89.     STRING s;
  90.     {
  91.     copyns( s, strlen(s) );
  92.     }
  93.  
  94. /* copyns(s) copies n characters from string s to stnd out
  95.    (conditionally). */
  96.  
  97. copyns(s,n)
  98.     STRING s;
  99.     int n;
  100.     {
  101.     while( n-- > 0 ) copyc(*s++);
  102.     }
  103.  
  104. /* error(s) writes an error message to standard error and
  105.    quits (could have used abort instead).
  106.  */
  107.  
  108. error(s)
  109.     STRING s;
  110.     {
  111.     fprintf( stderr, "ident: %s\n", s );
  112.     exit();
  113.     }
  114.  
  115. /*  strinb( s, buf ) returns true (1) if string s lies in buffer
  116.     buf  */
  117.  
  118. strinb(s,buf)
  119.     STRING s, buf;
  120.     {
  121.     while( *s )
  122.     if( *s++ != *buf++ ) return 0;
  123.     return 1;
  124.     }
  125.  
  126. /*  after_id( p, buf ) will return true (1) if an identifier
  127.     precedes pointer p. buf is the beginning of the buffer into
  128.     which p points.
  129.  */
  130.  
  131. after_id( p, buf )
  132.     STRING p, buf;
  133.     {
  134.     char c;
  135.  
  136.     while( p > buf )
  137.     {
  138.     c = *--p;
  139.     if( id_begin(c) ) return 1;
  140.     if( !id_cont(c) ) return 0;
  141.     }
  142.     return 0;
  143.     }
  144.  
  145. help()
  146.     {
  147.     p( "Format:" );
  148.     p( "" );
  149.     p( "ident  file-name  identifier  [replacement]" );
  150.     p( "" );
  151.     p( "will search the file for the occurrence of the identifier" );
  152.     p( "given as argument.  If a replacement is provided it will" );
  153.     p( "output the file onto standard out with all instances of the" );
  154.     p( "identifier replaced by the replacement.  If the replacement" );
  155.     p( "is not given, all lines bearing the identifier are printed" );
  156.     p( "to standard out together with the line numbers." );
  157.     p( "The significance of this program is that only identifiers" );
  158.     p( "will be matched.  Thus, if 'a' is given 'abc' or 'dada' " );
  159.     p( "will not match." );
  160.     exit();
  161.     }
  162.     
  163. p(s) STRING s;    
  164.     { fprintf( stderr, "\t%s\n" , s); }